import pylab as plt
import numpy as np
import sys
import networkx as nx
import os
import sys

from params import *

datasets = ['bkite','fsquare','livejournal', 'twitter']
lbls = ['Brightkite', 'Foursquare', 'LiveJournal', 'Twitter']

for dataset in datasets:
  tracefile = os.path.join(WORKDIR, 'gsn','results',dataset, '%s_node_geoclustering.txt'%dataset)

  clustering = []
  for line in open(tracefile):
      node,k,triangles,tot_triangles = line.split(';')
      t,t2 = map(float,(triangles,tot_triangles))
      c = 0.0
      if t2:
          c = t/t2
      clustering.append(c)
      
  print "read clustering for %d users"%(len(clustering))
  print dataset
  print 'Average geoclustering: ', sum(clustering)/len(clustering)
  print 'len clustering ', len(clustering)
  zero_values = sum(1 for x in clustering if x < 10**(-6))
  print 'zero values ', zero_values
  clustering = filter(lambda x: x > 0, clustering)
  print 'Average new geoclustering: ', sum(clustering)/len(clustering)

  bins = 1000
  pdf2,bins2,x = plt.hist(clustering,bins=bins,cumulative=False,normed=False)
  plt.close()

  c = [pdf2[0]]
  for j in range(1,len(pdf2)):
      t = c[j-1]
      c.append(t+pdf2[j])

  total = c[-1]
  print 'total ', total
  print 'total 2', len(clustering)
  c = map(lambda x: 1.0*x/total, c)

  plt.figure()
  plt.clf()
  plt.axes(FIG_AXES2)
  plt.semilogx(bins2[:-1],c,'k-')
  plt.ylabel('CDF')
  plt.xlabel('Geo-clustering coefficient')
  plt.grid(True)
  plt.axis([0.001,1,0,1])
  plt.savefig('%s_clustering.pdf'%dataset)
  plt.close()

  sys.exit()
